#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"

#include "light_demo.h"
#include "light.h"

#define TASK_STACK_SIZE 1024
#define TASK_PRIO 25

const unsigned char rgbDutyLow[] = {20,4,20};  //低亮度
const unsigned char rgbDutyHig[] = {99,99,99}; //高亮度

static void SelfAdaptionTask(void)
{   
    //三色灯初始化
    LightInit();

    for(;;)
    {
         int envLightVal =100- (1800-GetLightAverageVal(5))/9;
         if(envLightVal<=0)
        {
            envLightVal=1;
        }
        else if(envLightVal>=99)
        {
            envLightVal=99;
        }
        printf("envLightVal:%d \r\n",envLightVal);
        LightStart(envLightVal,envLightVal,envLightVal);
        usleep(200000);
    }

}

static void SelfAdaptionEntry(void)
{
    osThreadAttr_t attr;
    attr.name = "SelfAdaptionTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = TASK_STACK_SIZE;
    attr.priority = TASK_PRIO;

    if(osThreadNew((osThreadFunc_t)SelfAdaptionTask,NULL,&attr)==NULL)
    {
        printf("Fail to create SelfAdaptionTask! \r\n");
    }
    return ;
}
APP_FEATURE_INIT(SelfAdaptionEntry);